class Solution {
    public int thirdMax(int[] nums) {
        int flag1,flag2,flag3;
        long max1,max2,max3;
        flag1=flag2=flag3=0;
        max1=max2=max3=Long.MIN_VALUE;

        for(int i:nums){
            if((long)i>max1){
                max1=(long)i;
                flag1=1;
            }
        }

        for(int i:nums){
            if((long)i>max2&&(long)i<max1){
                max2=(long)i;
                flag2=1;
            }
        }

        for(int i:nums){
            if((long)i>max3&&(long)i<max2){
                max3=(long)i;
                flag3=1;
            }
        }

        if(flag1+flag2+flag3==3)return (int)max3;
        return (int)max1;
    }
}
